package com.irdstudio.bsp.executor.core.dao;

import com.irdstudio.bsp.executor.core.dao.domain.SOrg;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 机构管理类-[表名: 机构管理(s_org)]
 * 通过JDBC实现基本的数据表操作(CRUD) 
 * @author 代码自动生成
 * @version 1.0
 * @date 2020-11-13
 */
public class SOrgDao {

	/* 连接对象 */
	Connection conn = null;	

	public SOrgDao(Connection conn){
		this.conn = conn;
	}


    /**
	* 新增一行
	* @return int
	*/
    public int insertSOrg(SOrg sOrg) throws SQLException {

        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement("insert into s_org ( "
                + "organno,suporganno,locate,organname,organshortform,enname,orderno,distno,launchdate,organlevel,fincode,state,organchief,telnum,address,postcode,control,arti_organno,distname,area_dev_cate_type,is_marketing,busiorganno"
                + ") values ("+ "?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?" +")"
                );
            ps.setObject(1, sOrg.getOrganno());
            ps.setObject(2, sOrg.getSuporganno());
            ps.setObject(3, sOrg.getLocate());
            ps.setObject(4, sOrg.getOrganname());
            ps.setObject(5, sOrg.getOrganshortform());
            ps.setObject(6, sOrg.getEnname());
            ps.setObject(7, sOrg.getOrderno());
            ps.setObject(8, sOrg.getDistno());
            ps.setObject(9, sOrg.getLaunchdate());
            ps.setObject(10, sOrg.getOrganlevel());
            ps.setObject(11, sOrg.getFincode());
            ps.setObject(12, sOrg.getState());
            ps.setObject(13, sOrg.getOrganchief());
            ps.setObject(14, sOrg.getTelnum());
            ps.setObject(15, sOrg.getAddress());
            ps.setObject(16, sOrg.getPostcode());
            ps.setObject(17, sOrg.getControl());
            ps.setObject(18, sOrg.getArtiOrganno());
            ps.setObject(19, sOrg.getDistname());
            ps.setObject(20, sOrg.getAreaDevCateType());
            ps.setObject(21, sOrg.getIsMarketing());
            ps.setObject(22, sOrg.getBusiorganno());
            return ps.executeUpdate();
        } catch (SQLException e) {
            throw new SQLException("insert SOrg is Wrong!" + e.getMessage());
        } finally {
            close(rs, null, ps);
        }
	}

    /**
	* 根据主键删除一行记录
	* @return int
	*/
    public int deleteByPk(SOrg sOrg) throws SQLException {

        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement("delete from s_org where 1=1"
                );
            return ps.executeUpdate();
        } catch (SQLException e) {
            throw new SQLException("delete SOrg is Wrong!" + e.getMessage());
        } finally {
            close(rs, null, ps);
        }
	}

    /**
	* 根据主键更新一行记录
	* @return int
	*/
    public int updateByPk(SOrg sOrg) throws SQLException {

        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement("update s_org set "
                + " organno = ? ,"
                + " suporganno = ? ,"
                + " locate = ? ,"
                + " organname = ? ,"
                + " organshortform = ? ,"
                + " enname = ? ,"
                + " orderno = ? ,"
                + " distno = ? ,"
                + " launchdate = ? ,"
                + " organlevel = ? ,"
                + " fincode = ? ,"
                + " state = ? ,"
                + " organchief = ? ,"
                + " telnum = ? ,"
                + " address = ? ,"
                + " postcode = ? ,"
                + " control = ? ,"
                + " arti_organno = ? ,"
                + " distname = ? ,"
                + " area_dev_cate_type = ? ,"
                + " is_marketing = ? ,"
                + " busiorganno = ? "
                + " where 1=1"
                );
            ps.setObject(1, sOrg.getOrganno());
            ps.setObject(2, sOrg.getSuporganno());
            ps.setObject(3, sOrg.getLocate());
            ps.setObject(4, sOrg.getOrganname());
            ps.setObject(5, sOrg.getOrganshortform());
            ps.setObject(6, sOrg.getEnname());
            ps.setObject(7, sOrg.getOrderno());
            ps.setObject(8, sOrg.getDistno());
            ps.setObject(9, sOrg.getLaunchdate());
            ps.setObject(10, sOrg.getOrganlevel());
            ps.setObject(11, sOrg.getFincode());
            ps.setObject(12, sOrg.getState());
            ps.setObject(13, sOrg.getOrganchief());
            ps.setObject(14, sOrg.getTelnum());
            ps.setObject(15, sOrg.getAddress());
            ps.setObject(16, sOrg.getPostcode());
            ps.setObject(17, sOrg.getControl());
            ps.setObject(18, sOrg.getArtiOrganno());
            ps.setObject(19, sOrg.getDistname());
            ps.setObject(20, sOrg.getAreaDevCateType());
            ps.setObject(21, sOrg.getIsMarketing());
            ps.setObject(22, sOrg.getBusiorganno());
            return ps.executeUpdate();
        } catch (SQLException e) {
            throw new SQLException("update SOrg is Wrong!" + e.getMessage());
        } finally {
            close(rs, null, ps);
        }

	}

    /**
	* 根据主键查询一行记录
	* @return int
	*/
    public SOrg queryByPk(SOrg sOrg) throws SQLException {
        PreparedStatement ps = null;
        ResultSet rs = null;
        SOrg result = null;
        try {
            ps = conn.prepareStatement("select organno,suporganno,locate,organname,organshortform,enname,orderno,distno,launchdate,organlevel,fincode,state,organchief,telnum,address,postcode,control,arti_organno,distname,area_dev_cate_type,is_marketing,busiorgannofrom s_org where 1=1 "
                );
            rs = ps.executeQuery();
            if (rs.next()) {
				result = new SOrg();
				result.setOrganno(rs.getString("organno"));
				result.setSuporganno(rs.getString("suporganno"));
				result.setLocate(rs.getString("locate"));
				result.setOrganname(rs.getString("organname"));
				result.setOrganshortform(rs.getString("organshortform"));
				result.setEnname(rs.getString("enname"));
				result.setOrderno(rs.getBigDecimal("orderno"));
				result.setDistno(rs.getString("distno"));
				result.setLaunchdate(rs.getString("launchdate"));
				result.setOrganlevel(rs.getBigDecimal("organlevel"));
				result.setFincode(rs.getString("fincode"));
				result.setState(rs.getBigDecimal("state"));
				result.setOrganchief(rs.getString("organchief"));
				result.setTelnum(rs.getString("telnum"));
				result.setAddress(rs.getString("address"));
				result.setPostcode(rs.getString("postcode"));
				result.setControl(rs.getString("control"));
				result.setArtiOrganno(rs.getString("arti_organno"));
				result.setDistname(rs.getString("distname"));
				result.setAreaDevCateType(rs.getString("area_dev_cate_type"));
				result.setIsMarketing(rs.getString("is_marketing"));
				result.setBusiorganno(rs.getString("busiorganno"));
			}
        } catch (SQLException e) {
            throw new SQLException("update SOrg is Wrong!" + e.getMessage());
        } finally {
            close(rs, null, ps);
        }
        return result;
	}

    /**
	* 根据 sOrg 参数列表查询 不分页
	* @return int
	*/
    public List<SOrg> queryAll(SOrg sOrg) throws SQLException {
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<SOrg> result = new ArrayList<>();
        try {
            String sql = "select organno,suporganno,locate,organname,organshortform,enname,orderno,distno,launchdate,organlevel,fincode,state,organchief,telnum,address,postcode,control,arti_organno,distname,area_dev_cate_type,is_marketing,busiorgannofrom s_org where 1=1 ";
                if (sOrg.getOrganno() != null) {
                    sql = sql + " AND organno =  '" + sOrg.getOrganno() + "'";
                }
                if (sOrg.getSuporganno() != null) {
                    sql = sql + " AND suporganno =  '" + sOrg.getSuporganno() + "'";
                }
                if (sOrg.getLocate() != null) {
                    sql = sql + " AND locate =  '" + sOrg.getLocate() + "'";
                }
                if (sOrg.getOrganname() != null) {
                    sql = sql + " AND organname =  '" + sOrg.getOrganname() + "'";
                }
                if (sOrg.getOrganshortform() != null) {
                    sql = sql + " AND organshortform =  '" + sOrg.getOrganshortform() + "'";
                }
                if (sOrg.getEnname() != null) {
                    sql = sql + " AND enname =  '" + sOrg.getEnname() + "'";
                }
                if (sOrg.getOrderno() != null) {
                    sql = sql + " AND orderno =  '" + sOrg.getOrderno() + "'";
                }
                if (sOrg.getDistno() != null) {
                    sql = sql + " AND distno =  '" + sOrg.getDistno() + "'";
                }
                if (sOrg.getLaunchdate() != null) {
                    sql = sql + " AND launchdate =  '" + sOrg.getLaunchdate() + "'";
                }
                if (sOrg.getOrganlevel() != null) {
                    sql = sql + " AND organlevel =  '" + sOrg.getOrganlevel() + "'";
                }
                if (sOrg.getFincode() != null) {
                    sql = sql + " AND fincode =  '" + sOrg.getFincode() + "'";
                }
                if (sOrg.getState() != null) {
                    sql = sql + " AND state =  '" + sOrg.getState() + "'";
                }
                if (sOrg.getOrganchief() != null) {
                    sql = sql + " AND organchief =  '" + sOrg.getOrganchief() + "'";
                }
                if (sOrg.getTelnum() != null) {
                    sql = sql + " AND telnum =  '" + sOrg.getTelnum() + "'";
                }
                if (sOrg.getAddress() != null) {
                    sql = sql + " AND address =  '" + sOrg.getAddress() + "'";
                }
                if (sOrg.getPostcode() != null) {
                    sql = sql + " AND postcode =  '" + sOrg.getPostcode() + "'";
                }
                if (sOrg.getControl() != null) {
                    sql = sql + " AND control =  '" + sOrg.getControl() + "'";
                }
                if (sOrg.getArtiOrganno() != null) {
                    sql = sql + " AND arti_organno =  '" + sOrg.getArtiOrganno() + "'";
                }
                if (sOrg.getDistname() != null) {
                    sql = sql + " AND distname =  '" + sOrg.getDistname() + "'";
                }
                if (sOrg.getAreaDevCateType() != null) {
                    sql = sql + " AND area_dev_cate_type =  '" + sOrg.getAreaDevCateType() + "'";
                }
                if (sOrg.getIsMarketing() != null) {
                    sql = sql + " AND is_marketing =  '" + sOrg.getIsMarketing() + "'";
                }
                if (sOrg.getBusiorganno() != null) {
                    sql = sql + " AND busiorganno =  '" + sOrg.getBusiorganno() + "'";
                }
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while (rs.next()) {
				SOrg obj = new SOrg();
				obj.setOrganno(rs.getString("organno"));
				obj.setSuporganno(rs.getString("suporganno"));
				obj.setLocate(rs.getString("locate"));
				obj.setOrganname(rs.getString("organname"));
				obj.setOrganshortform(rs.getString("organshortform"));
				obj.setEnname(rs.getString("enname"));
				obj.setOrderno(rs.getBigDecimal("orderno"));
				obj.setDistno(rs.getString("distno"));
				obj.setLaunchdate(rs.getString("launchdate"));
				obj.setOrganlevel(rs.getBigDecimal("organlevel"));
				obj.setFincode(rs.getString("fincode"));
				obj.setState(rs.getBigDecimal("state"));
				obj.setOrganchief(rs.getString("organchief"));
				obj.setTelnum(rs.getString("telnum"));
				obj.setAddress(rs.getString("address"));
				obj.setPostcode(rs.getString("postcode"));
				obj.setControl(rs.getString("control"));
				obj.setArtiOrganno(rs.getString("arti_organno"));
				obj.setDistname(rs.getString("distname"));
				obj.setAreaDevCateType(rs.getString("area_dev_cate_type"));
				obj.setIsMarketing(rs.getString("is_marketing"));
				obj.setBusiorganno(rs.getString("busiorganno"));
                result.add(obj);
			}
        } catch (SQLException e) {
            throw new SQLException("update SOrg is Wrong!" + e.getMessage());
        } finally {
            close(rs, null, ps);
        }
        return result;
	}

    /**
	 * 关闭资源
	 * @param theRs
	 * @param theStmt
	 * @param thePs
	 */
    protected void close(ResultSet theRs, Statement theStmt, PreparedStatement thePs) {
        try {
            if (theRs != null)
                theRs.close();
            if (theStmt != null)
                theStmt.close();
            if (thePs != null)
                thePs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}
